﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.ComponentModel;

namespace Unleashed.AutoUpdate.Net
{
    public class DownloadEventArgs : BackgroundWorkerEventArgs
    {
        public Uri TargetUri { get; set; }
        public string TargetPath { get; set; }
        public ProxySettings Proxy { get; set; }
    }

    public class DownloadWorker : BackgroundWorker<DownloadEventArgs>
    {
        private WebClient client = new WebClient();

        public DownloadWorker()
        {
            client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(client_DownloadFileCompleted);
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        }

        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            OnProgressChanged(e);
        }

        void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            OnCompleted(new RunWorkerCompletedEventArgs(!e.Cancelled, e.Error, e.Cancelled));  // TODO
        }

        protected override void RunWorkerAsync(DownloadEventArgs e)
        {
            client.Proxy = e.Proxy;
            client.DownloadFileAsync(e.TargetUri, e.TargetPath);
        }
    }
}
